package de.voegel.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.security.sasl.AuthenticationException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import de.voegel.controller.MainControl;

public class Login extends JFrame implements Observer {

	private final JPanel contentPane;
	private final JTextField textField;
	private final JPasswordField passwordField;

	private final MainControl mainControl;
	private final JButton btnLogin;
	private final JButton btnRegister;
	private final JPanel panel;
	private final JTextField dbUser;
	private final JPasswordField dbPassword;

	/**
	 * Create the frame.
	 * 
	 * @param mainControl2
	 */
	public Login(final MainControl mainControl) {
		setTitle("Login");
		this.mainControl = mainControl;

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 266, 272);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Community-User",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(16, 11, 219, 74);
		contentPane.add(panel);
		panel.setLayout(null);

		JLabel lblBenutzer = new JLabel("Benutzer");
		lblBenutzer.setBounds(6, 19, 52, 16);
		panel.add(lblBenutzer);

		JLabel lblPasswort = new JLabel("Passwort");
		lblPasswort.setBounds(6, 48, 52, 16);
		panel.add(lblPasswort);

		textField = new JTextField();
		textField.setBounds(70, 16, 143, 22);
		panel.add(textField);
		textField.setText("user@db.de");
		textField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBounds(70, 47, 143, 20);
		panel.add(passwordField);
		passwordField.setText("user");

		btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String user = textField.getText();
				String password = passwordField.getText();
				String dbUsername = dbUser.getText();
				String dbPasswordText = dbPassword.getText();
				try {
					mainControl.auth(user, password, dbUsername,
							dbPasswordText, false);
				} catch (AuthenticationException e1) {
					mainControl.aceSmallException(e1);
				}
			}
		});
		btnLogin.setBounds(6, 199, 89, 23);
		contentPane.add(btnLogin);

		btnRegister = new JButton("Ende");
		btnRegister.setBounds(135, 199, 105, 23);
		btnRegister.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainControl.cancel();
			}
		});
		contentPane.add(btnRegister);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "DB-Benutzer",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(16, 96, 215, 96);
		contentPane.add(panel_1);
		panel_1.setLayout(null);

		JLabel lblBenutzer_1 = new JLabel("Benutzer");
		lblBenutzer_1.setBounds(6, 19, 46, 14);
		panel_1.add(lblBenutzer_1);

		dbUser = new JTextField();
		dbUser.setBounds(66, 16, 143, 20);
		panel_1.add(dbUser);
		dbUser.setColumns(10);

		JLabel lblPasswort_1 = new JLabel("Passwort");
		lblPasswort_1.setBounds(6, 45, 46, 14);
		panel_1.add(lblPasswort_1);

		dbPassword = new JPasswordField();
		dbPassword.setBounds(66, 42, 143, 20);
		panel_1.add(dbPassword);

		setLocationRelativeTo(null);

		setVisible(true);
	}

	@Override
	public void update(Observable o, Object arg) {

	}
}
